package com.xry.controller;

import com.mysql.cj.Session;
import com.xry.bean.ResetPwdDTO;
import com.xry.bean.User;
import com.xry.service.LoginService;
import com.xry.service.UserService;
import com.xry.util.CaptchaUtil;
import com.xry.util.Result;
import com.xry.util.SYS;
import com.xry.util.TokenUtil;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/login")
@CrossOrigin
public class LoginController {

    @Autowired
    LoginService loginService;
    @Autowired
    UserService userService;

    @PostMapping("/add")
    public Result add(@RequestBody User user){
        try{
            Object obj=userService.add(user);
            return Result.success("新增成功",obj);
        }catch (Exception e){
            return Result.fail(500,e.getMessage());
        }
    }

    @GetMapping("/captcha")
    public Object getCaptcha(){
        return Result.success(CaptchaUtil.getCaptcha());
    }
    @PostMapping("/dologin")
    public Result login(User user, @RequestParam("captcha") String captcha, @RequestParam("key") String key){
        //验证验证码
        if (!CaptchaUtil.checkCaptcha(key,captcha)){
            return Result.fail(500,"验证码错误");
        }
        //登录
        try {
            String token = loginService.login(user);
            //删除验证码
            return Result.success("登陆成功",token);
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail(500,e.getMessage());
        }
    }

    @GetMapping("/logininfo")
    public Result logininfo(HttpServletRequest request){
        String token = request.getHeader(SYS.TOKEN_AUTHORIZATION);
        User user = loginService.logininfo(token);
        return Result.success(user);

    }
    @PutMapping("/rest")
    public Result rest(@RequestBody ResetPwdDTO dto) {
        // 从DTO中获取参数（与前端参数名对应）
        String oldpassword = dto.getOldPwd();  // 前端oldPwd → 后端oldpassword
        String newpassword = dto.getNewPwd();  // 前端newPwd → 后端newpassword
        Integer id = dto.getUser_id();         // 前端user_id → 后端id
        try {
            User user = loginService.rest(oldpassword, newpassword, id);
            return Result.success("修改成功", user);
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail(500, e.getMessage());
        }
    }

    @PostMapping("/sendResetCode")
    public Result sendResetCode(@RequestBody Map<String, String> params) {
        String email = params.get("email");
        try {
            loginService.sendResetCode(email);
            return Result.success("验证码已发送至邮箱");
        } catch (Exception e) {
            return Result.fail(500, e.getMessage());
        }
    }

    @PostMapping("/resetByEmail")
    public Result resetByEmail(@RequestBody Map<String, String> params) {
        String email = params.get("email");
        String code = params.get("code");
        String newPassword = params.get("newPassword");
        try {
            loginService.resetByEmail(email, code, newPassword);
            return Result.success("密码重置成功，请登录");
        } catch (Exception e) {
            return Result.fail(500, e.getMessage());
        }
    }

}
